package com.nuotu.datawall.clouddata.util;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.nuotu.datawall.clouddata.been.ReaderCount;

import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

/**
 * Created by chenshijie on 2017/5/9.
 */

public class JsonUtil {
    //Result<Recommend> result = JsonUtil.fromJson(str,new TypeToken<Result<Recommend>>() {}.getType());
    private final static Gson gson = new Gson();

    public static <T> String toJson(T obj) {
        return gson.toJson(obj);
    }

    public static <T> T fromJson(String json, Type clz) {
        return gson.fromJson(json, clz);
    }

    public static <T> T mapToObj(Map map, Type clz) {
        return gson.fromJson(toJson(map), clz);
    }

    public static <T> List<T> fromArrayJson(String json, Type type) {
        //new TypeToken<List<ReaderCount>>() {}.getType()
        return gson.fromJson(json, type);
    }

}
